import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import Home from "@/views/Home.vue";
import { routes } from "./init";

const router = createRouter({
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: Home,
      // component: () => import("@/views/test.vue"),
    },
    {
      path: "/404",
      name: "404",
      component: () => import("@/views/404.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login.vue"),
    },
    {
      path: "/test",
      name: "test",
      component: () => import("@/views/test.vue"),
    },
    {
      path: "/initSelect",
      name: "initSelect",
      component: () => import("@/views/InitSelect.vue"),
    },
    {
      path: "/AppDownload",
      name: "AppDownload",
      component: () => import("@/views/AppDownload.vue"),
    },
    {
      path: "/Business",
      name: "Business",
      component: () => import("@/views/Business.vue"),
    },
    {
      path: "/help",
      name: "help",
      component: () => import("@/views/help/index.vue"),
    },
    ...routes,
  ],
});

// 路由跳转，没有的页面跳转到404
router.beforeEach((to, from, next) => {
  const matched = to.matched.length > 0;
  if (!matched) {
    // 如果没有匹配的路由记录，则重定向到 404 页面
    next({ path: "/404", replace: true });
  } else {
    // 否则继续导航
    next();
  }
});
export default router;
